package cn.schoolwow.workflow.test.module.oa.common.flow.step;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.DeployNodeType;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDeployNode;
import cn.schoolwow.workflow.test.module.oa.common.flow.common.ConvertAndSetDeployNodeTypeCommonFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.common.SetConvertWorkFlowNodeNameCommonFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.node.ConvertAuditNodeCommonFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.node.ConvertAutomaticNodeCommonFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.node.ConvertCarbonCopyNodeCommonFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.node.ConvertExclusiveGatewayCommonFlow;
import org.apache.commons.lang3.Validate;

public class ConvertChildNodeCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setWorkFlowDeployNode(flowContext);
        setAssigner(flowContext);
    }

    @Override
    public String name() {
        return "转换原始节点";
    }

    private void setWorkFlowDeployNode(FlowContext flowContext){
        flowContext.executeFlowList(new SetConvertWorkFlowNodeNameCommonFlow());
        String nodeName = flowContext.checkData("nodeName", String.class);
        Validate.notNull(nodeName, "节点名称不能为空");

        WorkFlowDeployNode workFlowDeployNode = new WorkFlowDeployNode();
        flowContext.putCurrentFlowInstanceData(workFlowDeployNode);

        workFlowDeployNode.name = nodeName;

        flowContext.executeFlowList(new ConvertAndSetDeployNodeTypeCommonFlow());
        DeployNodeType deployNodeType = flowContext.checkInstanceData(DeployNodeType.class);
        workFlowDeployNode.type = deployNodeType.value;
    }

    private void setAssigner(FlowContext flowContext){
        DeployNodeType deployNodeType = flowContext.checkInstanceData(DeployNodeType.class);

        switch (deployNodeType){
            case StartNode:
            case EndNode:
            case PassNode:
            case RejectNode:{
                flowContext.executeFlowList(new ConvertAutomaticNodeCommonFlow());
            }break;
            case ConditionNode:{}break;
            case AuditNode:{
                flowContext.executeFlowList(new ConvertAuditNodeCommonFlow());
            }break;
            case CarbonCopyNode:{
                flowContext.executeFlowList(new ConvertCarbonCopyNodeCommonFlow());
            }break;
            case ExclusiveGateway:{
                flowContext.executeFlowList(new ConvertExclusiveGatewayCommonFlow());
            }break;
            default:{
                throw new IllegalArgumentException("暂不支持的节点类型!类型:"+deployNodeType.name());
            }
        }
    }
}
